package com.hsmpay.mobile.service.added.impl;

import com.hsmpay.common.pojo.qian4.Qian4SendLog;
import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.mobile.dao.added.AddedOrderDao;
import com.hsmpay.mobile.dao.bank.BankCodeDao;
import com.hsmpay.mobile.dao.trans.Qian4SendLogDao;
import com.hsmpay.mobile.pojo.added.AddedOrder;
import com.hsmpay.mobile.pojo.bank.BankCode;
import com.hsmpay.mobile.service.added.AddedOrderService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Repository;
import com.hsmpay.qian4.pojo.PurchaseResult;

import javax.annotation.Resource;

/**
 * 合作银行业务接口实现类 bankService
 * 对应数据层 bankDao
 *
 * @author 颜铃璋
 * @date 2013-1-5
 */
@Repository("addedOrderService")
public class AddedOrderServiceImpl extends BaseServiceSupport<AddedOrder, Long> implements AddedOrderService<AddedOrder, Long> {
    @Resource(name = "addedOrderDao")
    private AddedOrderDao<AddedOrder, Long> addedOrderDao;
    @Resource(name = "qian4SendLogDao")
    private Qian4SendLogDao<Qian4SendLog, Long> qian4SendLogDao;
    @Resource(name = "bankCodeDao")
    private BankCodeDao<BankCode, Long> bankCodeDao;

    /**
     * 增值业务插入订单 及收款信息
     *
     * @param send
     * @param addedOrder
     * @return 返回日志ID
     * @throws Exception
     */
    public Long insertOrder(Qian4SendLog send, AddedOrder addedOrder) throws Exception {
        Long flag = qian4SendLogDao.insertEntity(send);
        addedOrder.setChannelId(send.getCollectionType().longValue());//支付通道代码
        addedOrder.setMerchantId(send.getMerchantId());//支付商户号
        addedOrder.setTerminalId(send.getTerminalId());//支付终端号
        Long orderId = addedOrderDao.insertEntity(addedOrder);
        addedOrder.setId(orderId);
        return flag;
    }


    /**
     * 解析收款xml
     *
     * @param order
     * @param xml
     * @return
     * @throws Exception
     */
    public AddedOrder purchaseResolveXml(AddedOrder order, String xml) throws Exception {
        Document doc = DocumentHelper.parseText(xml);
        Element root = doc.getRootElement();
//		if(null != root.element("transTime"))
//			order.setBankTransTime(root.element("transTime").getText());//交易时间
//		if(null != root.element("acquirers"))
//			order.setAcquirers(root.element("acquirers").getText());//收单行号
//		if(null != root.element("acquirersName"))
//			order.setAcquirersName(root.element("acquirersName").getText());//收单行名称
        if (null != root.element("issueBank"))
            order.setIssueBank(root.element("issueBank").getText());//发卡行名称
        if (null != root.element("cardType"))
            order.setCardType(root.element("cardType").getText());//卡类别
        if (null != root.element("expireDate"))
            order.setExpireDate(root.element("expireDate").getText());//有效期
        if (null != root.element("transId"))
            order.setBankOrderNum(root.element("transId").getText());//交易流水号
        if (null != root.element("batchId"))
            order.setBatchId(root.element("batchId").getText());//批次号
        if (null != root.element("searchId"))
            order.setSearchId(root.element("searchId").getText());//查询号
        if (null != root.element("authNum"))
            order.setAuthorizationCode(root.element("authNum").getText());//授权号
        if (null != root.element("refId"))
            order.setBankRef(root.element("refId").getText());//参考和
//		if(null != root.element("mac"))
//			order.setMac(root.element("mac").getText());//报文鉴别码
        order.setReqCode(root.element("respCode").getText());//应答码
        order.setReqMsg(root.element("respDesc").getText());//应答码描述

        if ("0000".equals(order.getReqCode())) {
            order.setStatus(4);//支付成功——等待业务缴费
        } else {
            order.setStatus(0);
        }

        addedOrderDao.updateEntity(order);
        return order;
    }

    /**
     * 解析收款xml
     *
     * @param order
     * @param xml
     * @return
     * @throws Exception
     */
    public AddedOrder purchaseResolveResult(AddedOrder order, Qian4SendLog send, PurchaseResult pr) throws Exception {
        order.setIssueBank(pr.getIssueBank());//发卡行名称
        if (StringUtils.isEmpty(pr.getIssueBank())) {
            //查询发卡行
            String issueBank = bankCodeDao.searchBankNameByCard(order.getBankCard());
            order.setIssueBank(issueBank);
        }
        order.setCardType(pr.getCardType());//卡类别
        order.setExpireDate(pr.getExpireDate());//有效期
        order.setBankOrderNum(pr.getBankOrderNum());//交易流水号
        order.setBatchId(pr.getBatchId());//批次号
        order.setSearchId(pr.getSearchId());//查询号
        order.setAuthorizationCode(pr.getAuthorizationCode());//授权号
        order.setBankRef(pr.getBankRef());//参考和
        order.setReqCode(pr.getReqCode());//应答码
        order.setReqMsg(pr.getReqMsg());//应答码描述

        if ("0000".equals(order.getReqCode())) {
            order.setStatus(4);//支付成功——等待业务缴费
        } else {
            order.setStatus(0);
        }
        addedOrderDao.updateEntity(order);

        //修改日志信息
        send.setIssueBank(pr.getIssueBank());//发卡行名称
        send.setCardType(pr.getCardType());//卡类别
        send.setExpireDate(pr.getExpireDate());//有效期
        send.setBankOrderNum(pr.getBankOrderNum());//交易流水号
        send.setBatchId(pr.getBatchId());//批次号
        send.setSearchId(pr.getSearchId());//查询号
        send.setAuthorizationCode(pr.getAuthorizationCode());//授权号
        send.setBankRef(pr.getBankRef());//参考和
        send.setResponseCode(pr.getReqCode());//应答码
        send.setBankMessage(pr.getReqMsg());//应答码描述
        qian4SendLogDao.updateByResult(send);
        return order;
    }

    /**
     * 我要收款 解析json  通道2
     *
     * @param transPs
     * @param xml
     * @param orderTypeId
     * @param orgId
     * @param clientType
     * @return
     * @throws Exception
     */
    public AddedOrder accountResolveJson(AddedOrder order, JSONObject json, Long orgId, Integer clientType) throws Exception {
        String RspCd = json.getString("RspCd");//银联应答码
        String RspMsg = json.getString("RspMsg");//银联错误信息
        String ReferNO = json.getString("ReferNO");//交易参考号
        //String TransDate = json.getString("TransDate");//交易日期  日期可以不保存
        //String TransTime = json.getString("TransTime");//交易时间
        //String SettDate = json.getString("SettDate");//清算日期

        if ("00".equals(RspCd)) {
            RspCd = "0000";
        }
        order.setReqCode(RspCd);
        order.setReqMsg(RspMsg);
        order.setBankRef(ReferNO);//参考号
        //order.setBankTransTime(TransTime);//交易时间

        if ("0000".equals(order.getReqCode())) {//银联处理成功
            //查询发卡行
            String issueBank = bankCodeDao.searchBankNameByCard(order.getBankCard());
            order.setIssueBank(issueBank);
        }
        addedOrderDao.updateEntity(order);
        return order;
    }

    /**
     * 订单支付失败时修改 订单
     *
     * @param id
     * @param status
     */
    public int receivablesError(Long id, Integer status, String reqCode, String reqMsg) throws Exception {
        AddedOrder order = new AddedOrder();
        order.setId(id);
        order.setStatus(status);
        order.setReqCode(reqCode);
        order.setReqMsg(reqMsg);
        return addedOrderDao.updateEntity(order);
    }

    /**
     * 修改第三方 状态码  和描述
     *
     * @param id
     * @param status
     * @param thirdCode
     * @param thirdMsg
     * @return
     * @throws Exception
     */
    public int updateThirdCodeAndMsg(Long id, Integer status, String thirdCode, String thirdMsg, String thirdOrderNum) throws Exception {
        AddedOrder order = new AddedOrder();
        order.setId(id);
        order.setStatus(status);
        order.setThirdCode(thirdCode);
        order.setThirdMsg(thirdMsg);
        order.setThirdOrderNum(thirdOrderNum);
        return addedOrderDao.updateEntity(order);
    }

    /**
     * 修改撤销
     *
     * @param id
     * @param status
     * @param revoCode
     * @param revoMsg
     * @param revoOrderNum
     * @return
     * @throws Exception
     */
    public int updateRevoCodeAndMsg(Long id, Integer status, String revoCode, String revoMsg, String revoOrderNum) throws Exception {
        AddedOrder order = new AddedOrder();
        order.setId(id);
        order.setStatus(status);
        order.setRevoCode(revoCode);
        order.setRevoMsg(revoMsg);
        order.setRevoOrderNum(revoOrderNum);
        return addedOrderDao.updateEntity(order);
    }

    /**
     * 修改第三方参数
     *
     * @param id
     * @param thirdParamMap
     * @throws Exception
     */
    public int updateThirdData(Long id, String thirdData) throws Exception {
        AddedOrder order = new AddedOrder();
        order.setId(id);
        order.setThirdData(thirdData);
        return addedOrderDao.updateEntity(order);
    }

}
